package com.qf.controller;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fileUpload")
public class FileUplaodController {

    @Autowired
    private FastFileStorageClient client;

    @Value("${fdfs.path}")
    private String nginxPath;

    @RequestMapping("/upload")
    public String upload(MultipartFile file) {


        try {

            // 1.获取文件的后缀
            String extension = FilenameUtils.getExtension(file.getOriginalFilename());

            // 2.把文件上传到FAstDFS中
            StorePath storePath = client.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), extension, null);

            // 3.获取文件的路径
            String fullPath = storePath.getFullPath();

            return nginxPath + fullPath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }
}
